// Copyright (c) 2020 Presto Labs Pte. Ltd.
// Author: taekwon

#pragma once

#include <rapidjson/writer.h>

#include <string>
#include <list>
#include <vector>

class JsonWriter {
 public:
  JsonWriter() : writer_(str_buf_) {
    writer_.StartObject();
  }

  void PutString(const char* key, const std::string& value) {
    writer_.Key(key);
    writer_.String(value.c_str());
  }

  void PutInt(const char* key, int value) {
    writer_.Key(key);
    writer_.Int(value);
  }

  void PutInt64(const char* key, int64_t value) {
    writer_.Key(key);
    writer_.Int64(value);
  }

  void PutArray(const char* key, const std::list<std::string>& array) {
    writer_.Key(key);
    writer_.StartArray();
    std::list<std::string>::const_iterator it = array.begin();
    while (it != array.end()) {
      writer_.String((*it).c_str());
      ++it;
    }
    writer_.EndArray();
  }

  std::string ToJsonString() {
    writer_.EndObject();
    std::string res = str_buf_.GetString();
    return res;
  }

 private:
  rapidjson::StringBuffer str_buf_;
  rapidjson::Writer<rapidjson::StringBuffer> writer_;
};
